package mx.ipn.presentacion.nomina;

import mx.ipn.presentacion.ConexionConNegocios;
import mx.ipn.presentacion.nomina.ui.*;
import mx.ipn.to.*;

import java.sql.*;
import com.trolltech.qt.gui.QDialog;
import com.trolltech.qt.gui.QMessageBox;
import com.trolltech.qt.gui.QTableWidgetItem;

public class VerTablas extends QDialog{
	Ui_Tablas crear;
	RenglonTO  renglon;
	TablaTO  tabla;
	RangoTO rango;
	int id_rango;
	double inicio;
	double fin;
	double porcentaje;
	double valorfijo;
	ResultSet res=null;
	ResultSet resreng=null;
	QTableWidgetItem newItem=null;
	String datos[] = new String[6];
	int idtabla=0;
	int idrenglon=0;
	public VerTablas(){
			crear = new Ui_Tablas();
			crear.setupUi(this);
			//rep.tableWidget.setRowCount(1);
			crear.tableWidget.itemClicked.connect(this,"selectr()");
			//rep.pushButton_2.clicked.connect(this, "buscar()");
			//rep.radioButton.clicked.connect(this,"mes()");
			//rep.radioButton_2.clicked.connect(this,"dia()");
			//crear.pushButton_2.clicked.connect(this, "eliminar()");
			crear.pushButton.clicked.connect(this, "llenatabla()");
			crear.pushButton_2.clicked.connect(this, "accept()");
            crear.pushButton_3.clicked.connect(this, "eliminar()");
            crear.pushButton_4.clicked.connect(this, "actualiza()");
            renglon = new RenglonTO();
            tabla= new TablaTO();
            llenacombo();
			//llenatabla();
			this.show();
	}
	public VerTablas(QDialog parent){
			
	}
	public void limpia(){
		crear.tableWidget.clearContents();
	}
	public void eliminar(){
		String query="";
		try{
			/*query ="select id_renglon from renglon where id_tablas="+idtabla;
			renglon.setQuery(query);
			res = renglon.getDatos();
			if(res.first())
				idrenglon= res.getInt("id_renglon");*/
			renglon.eliminar(String.valueOf(idrenglon));
			llenatabla();
	         }
		catch(Exception e){
			 QMessageBox.critical(null, "Error",
						"No se borro nungun dato.",
						QMessageBox.StandardButton.Ok,
					QMessageBox.StandardButton.Ok);}
	}
	public void selectr(){
		QTableWidgetItem newItem=null;
		int fila = crear.tableWidget.currentRow();
		newItem = crear.tableWidget.item(fila,0);
		idrenglon=(Integer.parseInt(newItem.text()));
		crear.pushButton_4.setEnabled(true);
		crear.pushButton_3.setEnabled(true);
	}
	public void actualiza(){
		   Double num= null;
		   datos[0] = String.valueOf(idrenglon);
		   datos[1] =String.valueOf(idtabla);
		   int fila = crear.tableWidget.rowCount();
		   newItem = crear.tableWidget.item(fila-1,1);
		   num= new Double(newItem.text()); 
		   inicio = num.doubleValue();
		   datos[2] =String.valueOf(inicio);
		   newItem = crear.tableWidget.item(fila-1,2);
		   num= new Double(newItem.text());
		   fin = num.doubleValue();
		   datos[3] = String.valueOf(fin);
		   newItem = crear.tableWidget.item(fila-1,3);
		   num= new Double(newItem.text());
		   valorfijo = num.doubleValue();
		   datos[4] = String.valueOf(valorfijo);
		   newItem = crear.tableWidget.item(fila-1,4);
		   num= new Double(newItem.text());
		   porcentaje = num.doubleValue();
		   datos[5] = String.valueOf(porcentaje);
				  if(valida()&&rango!=null){
				       renglon.guardar(datos);
				           QMessageBox.information(null,"informacion","guardada");
				        crear.tableWidget.insertRow(crear.tableWidget.rowCount());
			      }
	}
	public void llenacombo(){
		try{
		tabla.setQuery("select * from tablas");
		res = tabla.getDatos();
		if(res!=null){
			while(res.next()){
		    crear.comboBox.addItem(String.valueOf(res.getString("nombre")));
			}
			res.close();
	    }
		}catch(Exception e){
			
		}
	}
	public void llenatabla(){
		String nombre = crear.comboBox.currentText();
		idtabla=0;
		String query="";
		try{
		int j=0;
		query ="select id_tablas from tablas where nombre='"+nombre +"'";
		tabla.setQuery(query);
		res = tabla.getDatos();
		if(res.first())
		idtabla= res.getInt("id_tablas");
		query="select * from renglon where id_tablas="+idtabla;
		renglon.setQuery(query);
		resreng=renglon.getDatos();
		if(resreng!=null){
			crear.tableWidget.clearContents();
			resreng.last();
			crear.tableWidget.setRowCount(resreng.getRow());
			resreng.beforeFirst();
		       //for(int i=0;i<arremp.length;i++)
		        //	crear.tableWidget.insertRow(i);
			while(resreng.next()){
				newItem = new QTableWidgetItem(String.valueOf(resreng.getInt("id_ranglon")));
	    	    crear.tableWidget.setItem(j,0,newItem);
	    	    newItem = new QTableWidgetItem(String.valueOf(resreng.getDouble("inicio")));
	    	    crear.tableWidget.setItem(j,1,newItem); 
	    	    newItem = new QTableWidgetItem(String.valueOf(resreng.getDouble("fin")));
	    	    crear.tableWidget.setItem(j,2,newItem); 
	    	    newItem = new QTableWidgetItem(String.valueOf(resreng.getDouble("valor_fijo")));
	    	    crear.tableWidget.setItem(j,3,newItem); 
	    	    newItem = new QTableWidgetItem(String.valueOf(resreng.getDouble("porcentaje")));
	    	    crear.tableWidget.setItem(j,4,newItem);
	    	    j++;
			}
			res.close();
			crear.tableWidget.insertRow(crear.tableWidget.rowCount());
	    }
		}catch(Exception e){
			System.out.println(e.getMessage()+ "error en insertar factura");
	          
		}
	}
	public  static boolean isDouble(String cadena){
		Double num= null;
		try {
			num= new Double(cadena);
			return true;
		} catch (Exception nfe){
			return false;
		}
	}
	public boolean valida(){
		boolean valido=false;
		int fila = crear.tableWidget.rowCount()-1;
		newItem = crear.tableWidget.item(fila,1);
		if(isDouble(newItem.text())){
		   newItem = crear.tableWidget.item(fila,2);
		   if(isDouble(newItem.text())){
		      newItem = crear.tableWidget.item(fila,3);
		      if(isDouble(newItem.text())){
		         newItem = crear.tableWidget.item(fila,4);
		         if(isDouble(newItem.text())){
		        	 valido=true;
		           }
		         }
		      }
		   }
		return valido;
	}
	public void accept(){
	  
	   Double num= null;
	   datos[0] = "0";
	   datos[1] =String.valueOf(idtabla);
	   int fila = crear.tableWidget.rowCount();
	   newItem = crear.tableWidget.item(fila-1,1);
	   num= new Double(newItem.text()); 
	   inicio = num.doubleValue();
	   datos[2] =String.valueOf(inicio);
	   newItem = crear.tableWidget.item(fila-1,2);
	   num= new Double(newItem.text());
	   fin = num.doubleValue();
	   datos[3] = String.valueOf(fin);
	   newItem = crear.tableWidget.item(fila-1,3);
	   num= new Double(newItem.text());
	   valorfijo = num.doubleValue();
	   datos[4] = String.valueOf(valorfijo);
	   newItem = crear.tableWidget.item(fila-1,4);
	   num= new Double(newItem.text());
	   porcentaje = num.doubleValue();
	   datos[5] = String.valueOf(porcentaje);
			  if(valida()&&rango!=null){
			       renglon.guardar(datos);
			           QMessageBox.information(null,"informacion","guardada");
			        crear.tableWidget.insertRow(crear.tableWidget.rowCount());
		      }
	}
	public void reject(){
		this.dispose();
	}
}
